<?php

//Start session
session_start();

//Stop any error reporting
//error_reporting(0);

$mClass = new mClass();
class mClass
{
	public function MySQLConn()
	{
        $con = mysql_connect("localhost:3306", "root", "abcdef");
        $con = mysql_select_db("aleeda");
        return $con;
	}
	public function returnName()
	{
        $name = "Aleeda";
        return $name;
	}
	public function sys_print($str)
	{
        echo $str."\n";
	}
	public function userSelect($session, $request)
	{
        $query = mysql_query("SELECT ".$request." FROM users WHERE username = '".$session."'") or die("");
        $query = mysql_result($query, 0);
        return $query;
	}
	public function cmsGetString($where)
	{
        $query = mysql_query("SELECT str_content FROM cms_strings WHERE str_key = '".$where."'") or die("");
        $query = mysql_result($query, 0);
        return $query;
	}
	public function loginStartSession($username, $password)
	{
        $user = mysql_real_escape_string($username);
        $pass = sha1(mysql_real_escape_string($password));
        
        $query = mysql_query("SELECT * FROM users WHERE username = '".$user."'") or die("");
        $query = mysql_fetch_array($query);
  
        if($pass != $query['password'] || $user != $query['username'])
        	{
                echo "Wrong details, please <a href='index.php'>try again</a>";
        	}
        elseif($pass == "")
        	{
                echo "Wrong details, please <a href='index.php'>try again</a>.";
        	}
        elseif($user == "")
        	{
                echo "Wrong details, please <a href='index.php'>try again</a>.";
        	}
        else
        	{
				//Start session
				$_SESSION['name'] = $user;
				
				//Redirect to /me page
				header("Location: me");
        	}
	}
	public function listNews()
	{
        $result = mysql_query("SELECT * FROM cms_news_articles ORDER BY id ASC");
        while($row = mysql_fetch_array($result))
        	{
				$this->sys_print('<div class="topstory" style="background-image: url('.$row["topstory"].')">');
				$this->sys_print('<h4>Latest news</h4>');
				$this->sys_print('<h3><a href="/articles/'.$row["id"].'">'.$row["title"].' </a></h3>');
				$this->sys_print('<p class="summary">');
				$this->sys_print($row["short_text"]);
				$this->sys_print('</p>');
				$this->sys_print('<p>');
				$this->sys_print('<a href="/articles/'.$row["id"].'">Read more &raquo;</a>');
				$this->sys_print('</p>');
				$this->sys_print('</div>');
        	}
	}
}

$mClass->MySQLConn();

?>